﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using ChinaCustoms.Qingdao.Workflow.Controls;

public partial class WorkItem : System.Web.UI.Page
{
    private bool isTriggerChooseNextStepByOtherButton = false;
    private int needRemoveItemNumber = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        WorkflowControler.Save += new ChinaCustoms.Qingdao.Workflow.Controls.WorkflowEventHandler(WorkflowControler_Save);
        WorkflowControler.Submit += new ChinaCustoms.Qingdao.Workflow.Controls.WorkflowEventHandler(WorkflowControler_Submit);
        WorkflowControler.ChanageNextStep += new ChinaCustoms.Qingdao.Workflow.Controls.WorkflowChangeNextStepEventHandler(WorkflowControler_ChooseNextStep);

        ////以下代码演示 使用 保存按钮,提交流转按钮
        //WorkflowControler.SubmitButton.Attributes["onclick"] = "javascript:alert('Hello,world!'); return false;";
        //WorkflowControler.SubmitButton.Attributes["onclick"] = "javascript:alert('Hello,world!');";
        //WorkflowControler.SaveButton.Attributes["onclick"] = "javascript:alert('Hello,world!');";

        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(this.Context.Request.QueryString["TaskGuid"]))
            {
                WorkflowTrackingLink.Visible = true;
                WorkflowTrackingLink.NavigateUrl = string.Format("~/WorkflowTracking.aspx?TaskGuid={0}", Request.QueryString["TaskGuid"]);
            }
        }
    }

    //========================================================================================
    //以下代码演示如何通过 编程的方式 取消某些流转项 需要演示时,请把下面方法内的注释去掉
    //注意 需要在PageLoad或者更早的时候 把此方法挂接在WorkflowControler的ChanageNextStep事件上
    //----------------------------------------------------------------------------------------
    void WorkflowControler_ChooseNextStep(object sender, ChinaCustoms.Qingdao.Workflow.Controls.WorkflowChangeNextStepEventArgs e)
    {
        //1.可以直接在页面加载的时候使用
        //ChooseNextStepDetail(e);

        ////2.当然也可以将其放入 this.IsPostBack== false语句内部
        //if (this.IsPostBack == false)
        //{
        //    ChooseNextStepDetail(e);
        //}

        ////3.也可以通过其他按钮等事件进行触发;注意isTriggerChooseNextStepByOtherButton的状态是在相应按钮里面进行的设置
        //if (this.isTriggerChooseNextStepByOtherButton == true)
        //{
        //    ChooseNextStepDetail(e);
        //}

        //4.下一步选择项可以 交替变换的问题
        if (this.needRemoveItemNumber >= 0)
        {
            this.ChooseNextStepDetail(e,this.needRemoveItemNumber);
        }
    }


    //本方法方法体内的逻辑仅供演示;需要根据具体项目,具体活动 进行相应调整;
    private void ChooseNextStepDetail(ChinaCustoms.Qingdao.Workflow.Controls.WorkflowChangeNextStepEventArgs e,int removedNumber)
    {
        //1.每次都从OriginalItemList属性内获取最新基本的ItemList集合
        ListItemCollection newListCollection = e.OriginalItemList;

        //2.对新的 newListCollection 进行各种操作,这个地方仅仅是一个DEMO,具体请按照原来的方式进行取舍选项
        if (newListCollection.Count > removedNumber)
        {
            newListCollection.RemoveAt(removedNumber);
        }

        //3.最后把操作后的 结果 赋给e.ItemList;同时也要把 IsDealed设置为true表示 选项参数已经被处理过
        e.ItemList = newListCollection;
        e.IsDealed = true;



        ////当前活动的名称 通过QueryString总能获取到
        //string thisActivityCodeName = Request.QueryString["ActivityCodeName"];

        ////接下来就是针对某一个活动,进行 取消流转项
        //if (thisActivityCodeName == "FirstApproval")
        //{
        //    List<ListItem> removedListItems = new List<ListItem>();

        //    foreach (ListItem li in e.ItemList)
        //    {
        //        string listItemValue = li.Value;
        //        string[] arrayOfListItemValue = listItemValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

        //        string nextActivityCodeName = string.Empty;
        //        //============================================================================================
        //        //说明:传入参数e内部的属性ListItem(e.ItemList[i])的值内容的格式如下: 0下一个流转类型,1下一个活动名称
        //        //更多说明 请参看《青岛海关工作流平台使用说明.doc》
        //        //我们可以使用第0个值进行判断,也可以针对 第一个值进行判断(不过在针对第一值判断的时候需要注意一点
        //        //就是并不是所有的ListItem都有这个值,因此需要判断一下length,再调用),也可以两个都判断一下
        //        //--------------------------------------------------------------------------------------------
        //        if (arrayOfListItemValue.Length > 1)
        //        {
        //            nextActivityCodeName = arrayOfListItemValue[1];
        //        }

        //        if (nextActivityCodeName == "SecondApproval")
        //        {
        //            WorkItemAction selectedWorkItemAction = (WorkItemAction)int.Parse(arrayOfListItemValue[0]);
        //            switch (selectedWorkItemAction)
        //            {
        //                case WorkItemAction.StartSubFlow:
        //                    break;
        //                case WorkItemAction.ApprovalForExitSubFlow:
        //                    break;
        //                case WorkItemAction.Approval:
        //                    break;
        //                case WorkItemAction.DisApproval:
        //                    break;
        //                case WorkItemAction.FinalJudgment:
        //                    break;
        //                case WorkItemAction.Back:
        //                    break;
        //                case WorkItemAction.Goto:
        //                case WorkItemAction.Next:
        //                    removedListItems.Add(li);
        //                    break;
        //                default:
        //                    break;
        //            }
        //        }

        //    }

        //    foreach (ListItem li in removedListItems)
        //    {
        //        e.ItemList.Remove(li);
        //    }

        //}
    }

    /// <summary>
    /// 工作项保存时事件处理程序
    /// </summary>
    void WorkflowControler_Save(object sender, ChinaCustoms.Qingdao.Workflow.Controls.WorkflowEventArgs e)
    {
        WorkflowControler.TaskName = this.TaskName.Text;
        WorkflowControler.TaskType = this.TaskType.Text;

        bool result = WorkflowControler.WorkflowSave();

        if (result)
            this.Context.Response.Redirect("~/FunctionPage.aspx", true);
    }

    /// <summary>
    /// 工作项流转时事件处理程序
    /// </summary>
    void WorkflowControler_Submit(object sender, ChinaCustoms.Qingdao.Workflow.Controls.WorkflowEventArgs e)
    {
        WorkflowControler.TaskName = this.TaskName.Text;
        WorkflowControler.TaskType = this.TaskType.Text;

        ////以下代码演示使用 下一步要执行的 活动列表中的 选中项
        //ListItem itemChoosen= WorkflowControler.NextActionSeletedItem;
        //string itemChoosenValue= itemChoosen.Value;

        bool result = WorkflowControler.WorkflowSubmit(e);

        if (result)
            this.Context.Response.Redirect("~/FunctionPage.aspx", true);
    }

    protected void btnChooseNextStep_Click(object sender, EventArgs e)
    {
        this.isTriggerChooseNextStepByOtherButton = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.needRemoveItemNumber = 0;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        this.needRemoveItemNumber = 1;
    }
}
